package com.origon360.converters;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.origon.entities.Deal;
import com.origon360.Services.CommonService;

@FacesConverter(value="dealConverter")
@ManagedBean(name="dealConverter")
@SessionScoped
public class DealConverty implements Converter {
	
	@EJB
	private CommonService commonService;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
			if(value == null || value.isEmpty())
			{
				return null;
			}	
			
			Deal deal = commonService.findDealByName(value);			
		return deal;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
			if(value == null)
			{
				return null;
			}
			
			if(value instanceof Deal)
			{
				return ((Deal)value).getDealType();
			}
		return value.toString();
	}

}
